<template>
    <el-dialog v-model="dialogFormVisible" :title="'审核' + type" width="30%" center>
        <div class="describe">是否同意该{{ type }}申请</div>
        <el-radio-group v-model="formData.status" class="ml-4">
            <el-radio value="同意" size="large">同意</el-radio>
            <el-radio value="否决" size="large">否决</el-radio>
        </el-radio-group>
        <!-- 修改 v-if 条件 -->
        <el-input type="textarea" placeholder="请输入审核意见" v-model="formData.audit_opinion_a"
            v-if="userStore.identity === '管理员' || userStore.identity === '审核小组'" />
        <!-- 修改 v-else-if 条件 -->
        <el-input type="textarea" placeholder="请输入审核意见" v-model="formData.audit_opinion_b"
            v-else-if="userStore.identity === '辅导员'" />
        <!-- 修改 v-else-if 条件 -->
        <el-input type="textarea" placeholder="请输入审核意见" v-model="formData.audit_opinion_c"
            v-else-if="userStore.identity === '超级管理员' || userStore.identity === '院领导'" />
        <template #footer>
            <span class="dialog-footer">
                <!-- 修改 v-if 条件 -->
                <el-button type="primary" @click="bursaryAudit"
                    v-if="userStore.identity === '管理员' || userStore.identity === '审核小组'">
                    确定
                </el-button>
                <el-button type="primary" @click="FDYAudit" v-else-if="userStore.identity === '辅导员'">
                    确定
                </el-button>
                <el-button type="primary" @click="YLDAudit"
                    v-else-if="userStore.identity === '超级管理员' || userStore.identity === '院领导'">
                    确定
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { onBeforeUnmount, ref, reactive } from 'vue'
import {
    bus
} from "@/utils/mitt.js"
import { ElMessage } from 'element-plus'
import {
    useUserInfoStore
} from '@/store/userinfo.js'
import { groupAudit, counselorAudit, deanAudit ,sendVerificationCode} from '@/api/apply.js'
import { tracking } from '@/utils/operation.js'

const userStore = useUserInfoStore()
//奖学金 or 助学金
const type = ref('')

bus.on('bursaryInfo', (row: any) => {
    type.value = '助学金'
    formData.id = row.id
    formData.type = 'Bursary'
    formData.student_name = row.student_name
    formData.mobile = row.mobile
    console.log(userStore.identity)
    if (userStore.identity === '管理员' || userStore.identity === '审核小组') {
        formData.audit_person_a = userStore.name
        formData.audit_person_b = ''
        formData.audit_person_c = ''
    } else if (userStore.identity === '辅导员') {
        formData.audit_person_b = userStore.name
        formData.audit_person_a = ''
        formData.audit_person_c = ''
    } else if (userStore.identity === '超级管理员' || userStore.identity === '院领导') {
        formData.audit_person_c = userStore.name
        formData.audit_person_b = ''
        formData.audit_person_a = ''
    }
})
bus.on('scholarshipInfo', (row: any) => {
    type.value = '奖学金'
    formData.id = row.id
    formData.type = 'Scholarship'
    formData.student_name = row.student_name
    formData.mobile = row.mobile
    if (userStore.identity === '管理员' || userStore.identity === '审核小组') {
        formData.audit_person_a = userStore.name
        formData.audit_person_b = ''
        formData.audit_person_c = ''
    } else if (userStore.identity === '辅导员') {
        formData.audit_person_b = userStore.name
        formData.audit_person_a = ''
        formData.audit_person_c = ''
    } else if (userStore.identity === '超级管理员' || userStore.identity === '院领导') {
        formData.audit_person_c = userStore.name
        formData.audit_person_b = ''
        formData.audit_person_a = ''
    }
})

const formData = reactive({
    id: null,
    status: '',
    student_name: '',
    audit_person_a: '',
    audit_person_b: '',
    audit_opinion_a: '',
    audit_opinion_b: '',
    audit_person_c: '',
    audit_opinion_c: '',
    type: '',
    mobile:''
})

const emit = defineEmits(['success'])

const bursaryAudit = async () => {
    if (formData.status == "同意") {
        formData.status = 1
    } else {
        formData.status = -1
    }
    const res = await groupAudit(formData)
    if (res.data.status == 0) {
        ElMessage({
            message: '审核成功',
            type: 'success',
        })
        emit('success')
        tracking('审核小组', userStore.name, formData.student_name, '高级', '审核')
        dialogFormVisible.value = false
    } else {
        ElMessage.error('无法审核')
        dialogFormVisible.value = false
    }
}

const FDYAudit = async () => {
    if (formData.status == "同意") {
        formData.status = 2
    } else {
        formData.status = -2
    }
    const res = await counselorAudit(formData)
    if (res.data.status == 0) {
        ElMessage({
            message: '审核成功',
            type: 'success',
        })
        emit('success')
        tracking('辅导员', userStore.name, formData.student_name, '高级', '审核')
        dialogFormVisible.value = false
    } else {
        ElMessage.error('无法审核')
        dialogFormVisible.value = false
    }
}

const YLDAudit = async () => {
    if (formData.status == "同意") {
        formData.status = 3
    } else {
        formData.status = -3
    }
    const res = await deanAudit(formData)
    console.log(res)
    if (res.data.status == 0) {
        ElMessage({
            message: '审核成功',
            type: 'success',
        })
        emit('success')
        tracking('院领导', userStore.name, formData.student_name, '高级', '审核')
        const res = await sendVerificationCode(formData.mobile)
        dialogFormVisible.value = false
    } else {
        ElMessage.error(res.data.message)
        dialogFormVisible.value = false
    }
}

// 弹窗开关
const dialogFormVisible = ref(false)

// 打开弹窗
const open = () => {
    dialogFormVisible.value = true
}

defineExpose({
    open
})

onBeforeUnmount(() => {
    bus.all.clear()
})
</script>

<style lang="scss" scoped>
.describe {
    margin-bottom: 8px;
}
</style>